package com.lry.web.cartoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lry.web.cartoon.entity.Cartoon;
import com.lry.web.cartoon.mapper.CartoonMapper;
import com.lry.web.cartoon.service.CartoonChapterService;
import com.lry.web.cartoon.service.CartoonContentService;
import com.lry.web.cartoon.service.CartoonService;
import com.lry.web.cartoon.service.CartoonUpdateService;
import com.lry.web.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartoonServiceImpl implements CartoonService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    CartoonMapper cartoonMapper;
//    @Autowired
//    RestHighLevelClient restHighLevelClient;
    @Autowired
    CartoonChapterService cartoonChapterService;
    @Autowired
    CartoonContentService cartoonContentService;
    @Autowired
    CartoonUpdateService cartoonUpdateService;


    //根据条件在mysql查询漫画
    public Map<String,Object> getCartoons(String key, String option, Integer from, Integer size) throws IOException {
        Page<Cartoon> page = new Page<Cartoon>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        if("title".equals(option)){
            wrapper.like("title",key);
            wrapper.orderByAsc("length(title)");
        }else{
            wrapper.like(option,key);
            wrapper.orderByDesc("update_time");
        }
        cartoonMapper.selectPage(page,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("data",page.getRecords());
        map.put("count",page.getTotal());
        return map;
    }


    //根据CVId删除漫画,效率非常低,不推荐使用
    public int deleteByCVId(String cVId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cartoon_variable_id",cVId);
        cartoonChapterService.deleteByCVId(cVId);
        cartoonContentService.deleteByCVId(cVId);
        cartoonUpdateService.deleteByCVId(cVId);
        return cartoonMapper.delete(wrapper);
    }


    //发起采集请求到MQ
    public int spiderCartoon(String title) throws InterruptedException {
        rabbitTemplate.convertAndSend(RabbitMQConfig.GATHEREXCHANGE_NAME,"cartoon.gather",title);
        Thread.sleep(1000);
        while(RabbitMQConfig.code==0){
            Thread.sleep(1000);
        }
        int i = RabbitMQConfig.code;
        RabbitMQConfig.code = 0;
        return i;
    }
    //获取漫画总数量
    public Integer selectNum() {
        return cartoonMapper.selectNum();
    }

    //获取全部漫画，分页
    public Map<String,Object> selectCartoons(int from, int size,String option, String key) {
        Page<Cartoon> page = new Page<Cartoon>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("update_time");
        if(option !=null && key != null){
            wrapper.like(option,key);
        }
        cartoonMapper.selectPage(page, wrapper);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("cartoons",page.getRecords());
        map.put("pages",page.getPages());
        return map;
    }

    //更具CId删除漫画
    public int deleteByCartoonId(String cartoonId) throws Exception {
        List<String> listCId = Arrays.asList(cartoonId.split(","));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("cartoon_id",listCId);
        cartoonChapterService.deleteByCartoonId(cartoonId);
        cartoonContentService.deleteByCartoonId(cartoonId);
        cartoonUpdateService.deleteByCartoonId(cartoonId);
        return cartoonMapper.delete(wrapper);
    }

}
